package com.jdd.modules.people.util;

import com.jdd.common.util.oConvertUtils;
import com.jdd.dao.entity.TArea;
import com.jdd.modules.people.model.AreaIdModel;
import com.jdd.modules.people.model.TAreaTreeModel;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * <P>
 * 对应部门的表,处理并查找树级数据
 * <P>
 *
 */
@Slf4j
public class FindAreaChildrenUtil {


    private static List<AreaIdModel> idList = new ArrayList<>(4);

    /**
     * queryTreeList的子方法 ====1=====
     * 该方法是s将TArea类型的list集合转换成TAreaTreeModel类型的集合
     */
    public static List<TAreaTreeModel> wrapTreeDataToTreeList(List<TArea> recordList) {
     // 在该方法每请求一次,都要对全局list集合进行一次清理
        idList.clear();
        List<TAreaTreeModel> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            /*遍历区域，将每个区域转换为TAreaTreeModel添加导records中*/
            TArea area = recordList.get(i);
            records.add(new TAreaTreeModel(area));
        }
        List<TAreaTreeModel> tree = findChildren(records, idList);
        setEmptyChildrenAsNull(tree);
        log.info("Tree:{}"+tree);
        return tree;
    }


    public static List<AreaIdModel> wrapAreaIdModel() {
        return idList;
    }



    /**
     * queryTreeList的子方法 ====2=====
     * 该方法是找到并封装顶级父类的节点到TreeList集合
     */
    private static List<TAreaTreeModel> findChildren(List<TAreaTreeModel> recordList,
                                                         List<AreaIdModel> idList) {

        List<TAreaTreeModel> treeList = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            TAreaTreeModel branch = recordList.get(i);
            if (oConvertUtils.isEmpty(branch.getParentId())) {
                /*把没有ParentID的添加到treeList中*/
                treeList.add(branch);
                AreaIdModel areaIdModel = new AreaIdModel().convert(branch);
                /*把没有ParentId的添加到idList中*/
                idList.add(areaIdModel);
            }
        }
        getGrandChildren(treeList,recordList,idList);
        log.info("treeList:{}",treeList);
        return treeList;
    }

    /**
     * queryTreeList的子方法====3====
     *该方法是找到顶级父类下的所有子节点集合并封装到TreeList集合
     */
    private static void getGrandChildren(List<TAreaTreeModel> treeList,List<TAreaTreeModel> recordList,List<AreaIdModel> idList) {

        for (int i = 0; i < treeList.size(); i++) {
            TAreaTreeModel model = treeList.get(i);
            AreaIdModel idModel = idList.get(i);
            for (int i1 = 0; i1 < recordList.size(); i1++) {
                TAreaTreeModel m = recordList.get(i1);
                /*父节点不为null且父节点与遍历父节点相同*/
                if (m.getParentId()!=null && m.getParentId().equals(model.getId())) {
                    model.getChildren().add(m);
                    AreaIdModel dim = new AreaIdModel().convert(m);
                    idModel.getChildren().add(dim);
                }
            }
            getGrandChildren(treeList.get(i).getChildren(), recordList, idList.get(i).getChildren());
        }

    }


    /**
     * queryTreeList的子方法 ====4====
     * 该方法是将子节点为空的List集合设置为Null值
     */
    private static void setEmptyChildrenAsNull(List<TAreaTreeModel> treeList) {

        for (int i = 0; i < treeList.size(); i++) {
            TAreaTreeModel model = treeList.get(i);
            if (model.getChildren().size() == 0) {
                model.setChildren(null);
                model.setIsLeaf(true);
            }else{
                setEmptyChildrenAsNull(model.getChildren());
                model.setIsLeaf(false);
            }
        }
    }

}
